function applyplayer(){
	var TubeElement = $('.tubemp3');
	if(typeof TubeElement != 'undefined'){
		var hitAr = 0;
		while(hitAr < TubeElement.length){
			var tubes = $(TubeElement[hitAr]);
			var tubUrl = $(TubeElement[hitAr]).html();
			var type = tubes.attr('data-type');
			var play = tubes.attr('data-autoplay');
			var width = tubes.attr('data-width');
				var tubIdmatch = /watch\?v=(.*)/;
			var TubeID = tubIdmatch.exec(tubUrl);	
			TubeID = (typeof TubeID != 'undefined' && typeof TubeID[1] !='undefined')? TubeID[1]:'';
			if(
			typeof tubes != 'undefined' &&
			typeof tubUrl != 'undefined' &&
			typeof type != 'undefined' &&
			typeof play != 'undefined' &&
			typeof TubeID != 'undefined' &&
			typeof width != 'undefined' 
			)
			{
				tubes.removeAttr('data-type');
				tubes.removeAttr('data-autoplay');
				tubes.removeAttr('data-width');
				if(type =='small'){
					var height = 40;
					}else{
					var height = type.split('x');
					height = height[1];
				}
				widhtElem = Number(width) + 40;

				var player = '<div style="width:'+widhtElem+'px;margin:0px auto;"><iframe frameborder="0"'+
							'scrolling="no" width="'+width+'" height="'+height+'" '+
							'src="http://tubemp3script.com/player/playerembed.html#'+tubUrl+'|'+type+'|'+play+'"></iframe>'+
							'<a onclick="getConvert(\''+TubeID+'\')" href="#"><img src="player/download.png" /></div>';
				$(tubes).html(player);
			}
			
				hitAr++;
		}
	}
}
function jstubeCallback(Djson,key){
	var result =  Djson['data']['items'];
	if(typeof result=='object'){
		$(result).each(function(){
		var title = this['title'];
		var url =  'http://www.youtube.com/watch?v='+this['id'];
		var html = '<p><span class="tubeh2">'+title+'<span></p>'+
			'<div class="tubemp3" data-type="small" data-width="400" data-autoplay="none">'+
			''+url+''+
			'</div>';
		$($('.tubemp3result')[key]).append(html);
		});
	}
	
applyplayer();
}
function searchSTUB(title,key){
	var max = 4;
	var url = 'http://gdata.youtube.com/feeds/api/videos';
	var datareq = 'q='+escape(title)+'&max-results=4&alt=jsonc&v=2';
	$.ajax({
		url : url,
		data : datareq,
		type : 'GET',
		 dataType: 'json',
		success    : function(data){
					jstubeCallback(data,key);
                }
	});
	}
function applysearch(){
	var mytubeelement = $('.tubemp3result');
	if(typeof mytubeelement != 'undefined'){
	mytubeelement.each(function(key,value){
	var keyword = $(this).attr('data-keyword');
		if(typeof keyword != 'undefined'){
		$(this).removeAttr('data-keyword');
			searchSTUB(keyword, key);
		}	
	});
	}

}
function applysearchBox(){
$('#TubeSearchBox').submit(function(){
var keyword = $('#TubeSearchBox').find('input[name=s]').val();
var target = $('#TubeSearchBox').attr('action');
$(''+target+'').attr('data-keyword',keyword);
applysearch();
return false;
});
}
function closeFrame(){
$('#frameconvert').remove();
}
function getConvert(tubeid){
$('#frameconvert').remove();
var myhtml = '<div id="frameconvert" style="background:#ccc;z-index:1000;position:fixed;top:0px;right:0px;width:500px;height: 190px">' +
			'<iframe src="http://tubemp3script.com/work.php?v='+tubeid+'" width="100%" height="190" style="overflow:hidden;" frameborder="0"></iframe>'+
			'<button style="position:absolute; bottom:0px; right:0px; height:25px;font-size:10px;" onclick="closeFrame()">close</button></div>';
			$('body').append(myhtml);
}